<?php
###############   COPYLEFT GPLv3 LICENSE   ###############
##
## JFX Version 0.2.7
## Website Management Software
## www.jfxcms.com
##
## Copyright 2009 GPLv3 - http://www.opensource.org/licenses/gpl-3.0.html
##
## Anthony Gallon
## oi_antz@hotmail.com
##
## Permission is hereby granted to any person having a copy of this software
## to freely use and modify as required so long as the copyright notices
## and branding remain intact.
##
## Full license details available at http://www.jfxcms.com/license
##
###############   COPYLEFT GPLv3 LICENSE   ###############



if(get('add')==1){
    // adding a stylesheet association
    $keyname = preg_replace('/[^a-zA-Z0-9]/', '', get('js'));
    $pageid = (int) get('page');
    if($DB->countRows($CONFIG->dbprefix.'jfxjs_pages_to_javascripts', "js_key = '{$keyname}' AND page_id = '{$pageid}'")==0){
        $dbParams = array(
            'page_id' => $pageid,
            'js_key' => $keyname
        );
        $DB->insert($CONFIG->dbprefix.'jfxjs_pages_to_javascripts', $dbParams);
        JFX::addSuccess($this->lang('success'));
    }
}


if(get('remove')==1){
    // removing an association
    $keyname = preg_replace('/[^a-zA-Z0-9]/', '', get('js'));
    $pageid = (int) get('page');
    if($DB->countRows($CONFIG->dbprefix.'jfxjs_pages_to_javascripts', "js_key = '{$keyname}' AND page_id = '{$pageid}'")>0){
        $DB->delete($CONFIG->dbprefix.'jfxjs_pages_to_javascripts', "page_id = '{$pageid}' AND js_key = '{$keyname}'");
        JFX::addSuccess($this->lang('success'));
    }
}



if(get('sorted')==1){
    $pageid = (int) get('page');
    foreach($_POST['js'] as $sort=>$keyname){
        $keyname = preg_replace('/[^a-zA-Z0-9]/', '', $keyname);
        if($DB->countRows($CONFIG->dbprefix.'jfxjs_pages_to_javascripts', "js_key = '{$keyname}' AND page_id = '{$pageid}'")>0){
            $DB->update($CONFIG->dbprefix.'jfxjs_pages_to_javascripts', array('sorting'=>$sort), "js_key = '{$keyname}' AND page_id = '{$pageid}'");
        }
    }
    exit;
}





$pageid = (int) get('page');






$VIEW->addJs('
$(document).ready(function(){
     var sortableStop = function(){

        var sorted = $("#jfx-sort-associated").sortable("serialize");
        //alert(sorted);
        //$("#jfx-sort-pages").css("display", "none");
        //$("#jfx-workspace-inner").get(0).innerHTML = \'<div align="center"><img src="'.$CONFIG->imageUrl.'/admin/loadingAnimation.gif" alt="Loading..." /></div>\';
        $.post("'.$this->adminUrl.'/manage-javascript-associations/?page='.$pageid.'&sorted=1", sorted, function(){});
    }
    $("#jfx-sort-associated").sortable({cursor : "pointer", stop : sortableStop });
});
');









$allJs = $DB->fetchAll("SELECT * FROM {$CONFIG->dbprefix}jfxjs_javascripts ORDER BY keyname ASC");
$assignedJs = $DB->fetchAll("SELECT * FROM {$CONFIG->dbprefix}jfxjs_pages_to_javascripts WHERE page_id = '{$pageid}' ORDER BY sorting ASC");

$unassignedJs = array();

foreach($allJs as $k=>$v){
    $v['title'] = $this->getContentLang('title', $v['keyname']);

    if($DB->countRows($CONFIG->dbprefix.'jfxjs_pages_to_javascripts', "js_key = '{$v['keyname']}' AND page_id = '{$pageid}'")==0){
        $v['url'] = $this->adminUrl.'/manage-javascript-associations/?add=1&amp;page='.$pageid.'&amp;js='.$v['keyname'];
        $v['img'] = $this->imageUrl.'/add.png';
        $unassignedJs[] = $v;
    }
}

foreach($assignedJs as $k=>$v){
    if(!is_array($v) || trim($v['js_key'])==''){
        unset($assignedJs[$k]);
        continue;
    }
    $v = $DB->fetchRow("SELECT * FROM {$CONFIG->dbprefix}jfxjs_javascripts WHERE keyname = '{$v['js_key']}'");
    $v['title'] = $this->getContentLang('title', $v['keyname']);
    $v['url'] = $this->adminUrl.'/manage-javascript-associations/?remove=1&amp;page='.$pageid.'&amp;js='.$v['keyname'];
    $v['img'] = $this->imageUrl.'/delete.png';
    $assignedJs[$k] = $v;
}


$SMARTY->assign('assigned', $assignedJs);
$SMARTY->assign('unassigned', $unassignedJs);

echo $this->fetchTemplate('manage-javascript-associations.tpl');

